package com.drakulo.games.ais.core;

import java.math.BigDecimal;

/**
 * <h1>Helper for numbers manipulation</h1>
 * <p>
 * This helper take care of basic and common manipulations of BigDecimals
 * </p>
 * 
 * @author Drakulo
 * 
 */
public class NumberHelper {
	private static final BigDecimal HUNDRED = BigDecimal.valueOf(100);

	/**
	 * Make a division of two integers and returns a double with a precision of
	 * 2
	 * 
	 * @param divided
	 *            the int to divide
	 * @param divisor
	 *            the divisor
	 * @return the result of the division with a scale of 2
	 */
	public static double divide(int divided, int divisor) {
		BigDecimal bigDivided = BigDecimal.valueOf(divided);
		BigDecimal bigDivisor = BigDecimal.valueOf(divisor);

		BigDecimal result = bigDivided.divide(bigDivisor);
		return result.setScale(2).doubleValue();
	}

	/**
	 * @param d1
	 *            a BigDecimal
	 * @param d2
	 *            a BigDecimal
	 * @return d1 < d2
	 */
	public static boolean inf(BigDecimal d1, BigDecimal d2) {
		return d1.compareTo(d2) == -1;
	}

	/**
	 * @param d1
	 *            a BigDecimal
	 * @param d2
	 *            a BigDecimals
	 * @return d1 <= d2
	 */
	public static boolean infEq(BigDecimal d1, BigDecimal d2) {
		return inf(d1, d2) || d1.equals(d2);
	}

	/**
	 * @param d1
	 *            a BigDecimal
	 * @param d2
	 *            a BigDecimal
	 * @return d1 > d2
	 */
	public static boolean sup(BigDecimal d1, BigDecimal d2) {
		return !inf(d1, d2);
	}

	/**
	 * Set the BigDecimal scale to 2
	 * 
	 * @param bd
	 *            the number to scale
	 * @return the scaled number
	 */
	public static BigDecimal scale(BigDecimal bd) {
		return bd.setScale(2);
	}

	/**
	 * Utility method for classical rule of 3 resolutions. a -> b, c -> result
	 * 
	 * @param a
	 *            Param A
	 * @param b
	 *            Param B
	 * @param c
	 *            Param C
	 * @return the result of the equation
	 */
	public static int ruleOf3(int a, int b, int c) {
		BigDecimal ba = BigDecimal.valueOf(a);
		BigDecimal bb = BigDecimal.valueOf(b);
		BigDecimal bc = BigDecimal.valueOf(c);
		return ruleOf3(ba, bb, bc).intValue();
	}

	/**
	 * Utility method for classical rule of 3 resolutions. a -> b, c -> result
	 * 
	 * @param a
	 *            Param A
	 * @param b
	 *            Param B
	 * @param c
	 *            Param C
	 * @return the result of the equation
	 */
	public static BigDecimal ruleOf3(BigDecimal a, BigDecimal b, BigDecimal c) {
		if (BigDecimal.ZERO.intValue() == a.intValue()) {
			return BigDecimal.ZERO;
		}
		return c.multiply(b).divide(a, 0, BigDecimal.ROUND_DOWN);
	}

	/**
	 * Calculate the parcentage value of current param, according to the max
	 * param
	 * 
	 * @param max
	 *            the max value
	 * @param current
	 *            the current value
	 * @return the percentage
	 */
	public static BigDecimal getPercentage(BigDecimal max, BigDecimal current) {
		return ruleOf3(max, HUNDRED, current);
	}

	/**
	 * Calculate the parcentage value of current param, according to the max
	 * param
	 * 
	 * @param max
	 *            the max value
	 * @param current
	 *            the current value
	 * @return the percentage
	 */
	public static int getPercentage(int max, int current) {
		BigDecimal bmax = BigDecimal.valueOf(max);
		BigDecimal bcurrent = BigDecimal.valueOf(current);
		return ruleOf3(bmax, HUNDRED, bcurrent).intValue();
	}

	/**
	 * Formats the big decimal in order to separate thousands
	 * 
	 * @param d
	 *            - the number to format
	 * @return the formatted string
	 */
	public static String format(BigDecimal d) {
		String temp = d.toString();
		StringBuilder result = new StringBuilder();
		for (int i = temp.length(); i > 0; i--) {
			if (i % 3 == 0) {
				result.append(" ");
			}
			result.append(temp.charAt(i));
		}
		return result.toString();
	}
}
